<?php
/**
 + Created by:	Webit Solution
 * Company:		Webit Solution
 + Contact:		www.webitsolution.com , info@webitsolution.com
			www.webitsolution.com, developer@webitsolution.com
 * Created on:	Dec 10, 2012
 ^
 + Project: 		WRecruit 
 * File Name:	Pplugin/jstopjobs.php
 ^ 
 * Description: Plugin for WRecruit
 ^ 
 * History:		1.0.1 - Nov 27, 2012
 ^ 
 */

defined( '_JEXEC' ) or die( 'Restricted access' );
 
// Import Joomla! Plugin library file
jimport('joomla.plugin.plugin');
 
//The Content plugin Loadmodule
class plgContentJSTopJobs extends JPlugin
{
		// for joomla 1.5
		public function onPrepareContent( &$row, &$params, $page=0 )
        {
                if ( JString::strpos( $row->text, 'jstopjobs' ) === false ) {
                        return true;
                }

              // expression to search for
                $regex = '/{jstopjobs\s*.*?}/i';
                if ( !$this->params->get( 'enabled', 1 ) ) {
                        $row->text = preg_replace( $regex, '', $row->text );
                        return true;
                }
                preg_match_all( $regex, $row->text, $matches );
                $count = count( $matches[0] );
                if ( $count ) {
                        // Get plugin parameters
                        $style = $this->params->def( 'style', -2 );
                        $this->_process( $row, $matches, $count, $regex, $style );
                }
        }
		//for joomla 1.6
		public function onContentPrepare( $context, &$row, &$params, $page=0 )
        {
                if ( JString::strpos( $row->text, 'jstopjobs' ) === false ) {
                        return true;
                }

              // expression to search for
                $regex = '/{jstopjobs\s*.*?}/i';
                if ( !$this->params->get( 'enabled', 1 ) ) {
                        $row->text = preg_replace( $regex, '', $row->text );
                        return true;
                }
                preg_match_all( $regex, $row->text, $matches );
                $count = count( $matches[0] );
                if ( $count ) {
                        // Get plugin parameters
                        $style = $this->params->def( 'style', -2 );
                        $this->_process( $row, $matches, $count, $regex, $style );
                }
        }
        protected function _process( &$row, &$matches, $count, $regex, $style )
        {
                for ( $i=0; $i < $count; $i++ )
                {
                        $load = str_replace( 'jstopjobs', '', $matches[0][$i] );
                        $load = str_replace( '{', '', $load );
                        $load = str_replace( '}', '', $load );
                        $load = trim( $load );
 
                        $modules       = $this->_load( $load, $style );
                        $row->text         = preg_replace( '{'. $matches[0][$i] .'}', $modules, $row->text );
                }
                $row->text = preg_replace( $regex, '', $row->text );
        }

        protected function _load( $position, $style=-2 )
        {
                $document      = &JFactory::getDocument();
                $renderer      = $document->loadRenderer('module');
                $params                = array('style'=>$style);
 
                $db = JFactory::getDBO();
				$lang = & JFactory :: getLanguage();
				$lang->load('plg_content_jstopjobs', JPATH_ADMINISTRATOR);

				// Language variable start
				$companytitle = JText::_('JS_COMPANY');
				$categorytitle = JText::_('JS_CATEGORY');
				$typetitle = JText::_('JS_TYPE');
				$postedtitle = JText::_('JS_POSTED');
				// Language variable end
				
				//$curdate = date('Y-m-d H:i:s');
				$noofjobs = $this->params->get('noofjobs');
				$theme = $this->params->get('theme');
				// scs
				$componentAdminPath = JPATH_ADMINISTRATOR . DS . 'components' . DS . 'com_wrecruit';
				$componentPath =  'components' . DS . 'com_wrecruit';
				//$divclass=array('odd','even');

				require_once $componentPath . DS . 'models' . DS . 'mpwrecruit.php';
				$config = array(
					'table_path' => $componentAdminPath . DS . 'tables'
				);
				$model = new WRecruitModelMpWRecruit($config);
				$result = $model->getTopJobs($noofjobs,$theme);
				$jobs = $result[0];
				$trclass = $result[1];	
				$dateformat = $result[2];
				
				//sce
				
/*				
				$query = "SELECT * FROM ". $db->nameQuote('#__wrecruit_config')." WHERE configname = 'theme' OR configname = 'date_format' OR configname = 'data_directory' ";
				$db->setQuery($query);
				$configs = $db->loadObjectList();
				foreach($configs AS $config){
					if ($config->configname == 'theme')$css = $config->configvalue;
					if ($config->configname == 'date_format')$dateformat = $config->configvalue;
					if ($config->configname == 'data_directory')$datadirectory = $config->configvalue;
				}	
				if ($theme == 1){ // wrecruit theme
					$trclass = array("odd", "even");
					$headlinecss = 'pageheadline';
					$sortlinks = 'sortlnks';
					if (!$css) $css = 'wrecruit01.css'; 
					if ($css == 'templatetheme.css') $trclass = array("sectiontableentry1", "sectiontableentry2");
					$document =& JFactory::getDocument();
					$document->addStyleSheet('components/com_wrecruit/css/'.$css);
				}elseif ($theme == 2){ // template theme
					$trclass = array("sectiontableentry1", "sectiontableentry2");
				}else 	$trclass = array("", ""); //no theme

				$query = "SELECT job.id, job.title, job.jobcategory, job.created, cat.cat_title
					, company.id AS companyid, company.name AS companyname, jobtype.title AS jobtypetitle 
					FROM ". $db->nameQuote('#__wrecruit_jobs')." AS job 
					JOIN ". $db->nameQuote('#__wrecruit_categories')." AS cat ON job.jobcategory = cat.id 
					JOIN ". $db->nameQuote('#__wrecruit_jobtypes')." AS jobtype ON job.jobtype = jobtype.id 
					LEFT JOIN ". $db->nameQuote('#__wrecruit_companies')." AS company ON job.companyid = company.id 
					WHERE job.status = 1 AND job.startpublishing <= " . $db->Quote($curdate) . " AND job.stoppublishing >= " . $db->Quote($curdate) ."
					ORDER BY job.hits DESC LIMIT ".$noofjobs;
				$db->setQuery($query);
				$jobs = $db->loadObjectList();
*/
				$contents = '';
				$noofcols = $this->params->get('noofcols');
				$shtitle = $this->params->get('shtitle');
				$title = $this->params->get('title');
				$company = $this->params->get('company');
				$category= $this->params->get('category');
				$jobtype= $this->params->get('jobtype');
				$posteddate= $this->params->get('posteddate');
				$separator= $this->params->get('separator');
				$colwidth = 100 / $noofcols;
				//sce
				$sliding= $this->params->get('sliding','1');
				$consecutivesliding= $this->params->get('consecutivesliding','3');
				$slidingdirection= $this->params->get('slidingdirection','1'); // 0 = left  , 1=up
				if($this->params->get('Itemid')) $itemid = $this->params->get('Itemid');			
				else  $itemid =  JRequest::getVar('Itemid');
				//sce

				if (isset($jobs)) { 
				    $contents = '<table cellpadding="0" cellspacing="0" border="0" width="100%" class="contentpane">';
					$isodd = 1;
					$count = 1;
					if ($shtitle == 1){
							$contents .=  '<tr class="'.$trclass[$isodd].'">';
						$contents .=  '<td colspan="'.$noofcols.'">';
						$contents .=  '<h2><u>'.$title.'</u></h2>';	
						if ($separator == 1) $contents .= '<hr style="border:dashed #C0C0C0; border-width:1px 0 0 0; height:0;line-height:0px;font-size:0;margin:0;padding:0;">';
						$contents .= '</td>';
						$contents .= '</tr>';
					}	
					foreach ($jobs as $job) {
					    $isodd = 1 - $isodd;
						if ($count == 1){
							$contents .=  '<tr class="'.$trclass[$isodd].'">';
						}	
						$contents .=  '<td width="'.$colwidth.'%">'
							. '<a href="index.php?option=com_wrecruit&c=wrecruit&view=employer&layout=view_job&vj=2&oi=' . $job->id . '&Itemid='.$itemid.'"><u><strong>'
					        . $job->title . '</strong></u></a><br />';
						if ($company == 1) $contents .=  '<small>'.$companytitle.': <a href="index.php?option=com_wrecruit&c=wrecruit&view=employer&layout=view_company&vm=5&md=' .$job->companyid . '&jobcat=' .$job->jobcategory . '&Itemid='.$itemid.'">'.$job->companyname.'</a></small><br />';	
						if ($category == 1) $contents .= '<small>'.$categorytitle.': '.$job->cat_title.'</small><br />';	
						if ($jobtype == 1) $contents .= '<small>'.$typetitle.': '.$job->jobtypetitle.'</small><br />';	
						if ($posteddate == 1) $contents .= "<small>".$postedtitle.": ".date($dateformat,strtotime($job->created))."</small><br /><br />";
						if ($separator == 1) $contents .= '<hr style="border:dashed #C0C0C0; border-width:1px 0 0 0; height:0;line-height:0px;font-size:0;margin:0;padding:0;">';
						$contents .= '</td>';
						if ($count == $noofcols){
							$contents .= '</tr>';
							$count = 0;
						}	
						$count = $count + 1;
				    }
					if ($count-1 < $noofcols){
						for ($i = $count; $i <= $noofcols; $i++){
						$contents .= '<td></td>';
						}	
						$contents .= '</tr>';
					}	
					$contents .= '</table>';
				}
				//scs		
				if ($sliding == 1) {                    
					
					if($slidingdirection == 1 ){
						for ($a = 0; $a < $consecutivesliding; $a++){ 	$contents .= $contents; }
						$contents =  '<marquee  direction="up" scrollamount="2" onmouseover="this.stop();" onmouseout="this.start()";>'.$contents.'</marquee>';
						$contents = $contents.'<br clear="all">';
					}else{
						$tcontents = '<table cellpadding="0" cellspacing="0" border="1" width="100%" class="contentpane"> <tr>';
						for ($a = 0; $a < $consecutivesliding; $a++){ $scontents = '<td>'.$contents.'</td>'; }
						$contents = $tcontents.$scontents.'</tr></table>';
						$contents =  '<marquee  scrollamount="2" onmouseover="this.stop();" onmouseout="this.start()";>'.$contents.'</marquee>';
					}	
				}
				//sce

               return $contents;
        }
}



?>
